#include "switch_plugin.h"

#include "log.h"
#include "script_object_factory.h"
#include "switch_script_object.h"

extern Log g_logger;

NPError SwitchPlugin::Init(NPP instance, uint16_t mode, int16_t argc,
                           char *argn[], char *argv[],
                           NPSavedData *saved) {
  g_logger.WriteLog("Msg", "SwitchPlugin::Init");
  script_object_ = NULL;
  instance->pdata = this;
  return PluginBase::Init(instance, mode, argc, argn, argv, saved);
}

NPError SwitchPlugin::UnInit(NPSavedData **save) {
  g_logger.WriteLog("Msg", "SwitchPlugin::UnInit");
  PluginBase::UnInit(save);
  script_object_ = NULL;

  return NPERR_NO_ERROR;
}

NPError SwitchPlugin::GetValue(NPPVariable variable, void *value) {
  switch(variable) {
    case NPPVpluginScriptableNPObject:
      script_object_ = ScriptObjectFactory::CreateObject(
          get_npp(), SwitchScriptObject::Allocate);
      g_logger.WriteLog("GetValue", "GetValue");
      if (script_object_ != NULL)
        *(NPObject**)value = script_object_;
      else
        return NPERR_OUT_OF_MEMORY_ERROR;
      break;
    case NPPVpluginNeedsXEmbed:
      *(bool*)value = 1;
      break;
    default:
      return NPERR_GENERIC_ERROR;
  }

  return NPERR_NO_ERROR;
}